<template>
	<view class="around">
		<view class="list">
			
			<view class="left">
				<!-- cid:{{selceted_cid}} -->
				<!-- <view v-for="(val) in cate_list" :key="'cate_'+val.cid" @click="changeCateId(val.cid)">
					{{val.cname}}-{{val.cid}}
				</view> -->
				<!-- <view v-for="(val,index) in cate_list" :key="'cate_'+val.cid" @click="changeCateId(index)">
					{{val.cname}}-{{val.cid}}
				</view> -->
				
				<view 
				 v-for="(val,index) in cate_list" :key="'cate_'+val.cid" 
				 :class="val.cid == selceted_cid ? 'active' :''"
				 @click="changeCateId" 
				:data-cid="val.cid">
					{{val.cname}}
				</view>
				
				<!-- <view class="active">白酒</view>
				<view>啤酒</view> -->
			</view>
			
			<view class="right">
				<!-- {{show_list}} -->
				<!-- item start -->
				<!-- 4.从 store中获取数据 -->
				<view class="item" v-for="(item,index) in show_list" :key="'list_'+index">
					<view class="pic">
						<image :src="item.pic"></image>
					</view>
					<view class="center">
						<text>{{item.name}}</text>
						<view class="price">
							<text>{{item.price}}元</text>/瓶子
						</view>
					</view>
					<view class="btn">
						<image v-show="item.num>0" src="../../static/footer2.png" @click="subCart" :data-proid="item.id"></image>
						<text v-show="item.num>0">{{item.num}}</text>
						<image src="../../static/footer2-sel.png" @click="addCart" :data-pro="item"></image>
					</view>
				</view>
			    <!-- item end -->
				
				<!-- item start -->
				<!-- <view class="item">
					<view class="pic">
						<image src="../../static/7.png"></image>
					</view>
					<view class="center">
						<text>贵州茅台</text>
						<view class="">
							100元/瓶子
						</view>
					</view>
					<view class="btn">
						<image src="../../static/footer2.png"></image>
						<text>1</text>
						<image src="../../static/footer2-sel.png"></image>
					</view>
				</view> -->
				<!-- item end -->
				
			</view>	
		</view>
		
		<div class="cart">
			<div class="left">
				总数：{{cart.total_num}}，总价：{{cart.total_price}}
			</div>
			<div class="right">
				购买
			</div>
		</div>
	</view>
</template>

<script>
	import {mapMutations,mapState,mapGetters} from 'vuex';
	
	export default {
		data() {
			return {
				cate_list:[],//分类
				selceted_cid:1//默认分类
			};
		},
		computed:{
			...mapState(['around_list','cart']),
			...mapGetters(["show_list"])
		},
		methods:{
			...mapMutations(['SETCATELIST','SETCART']),
			/* changeCateId(cid){
				this.selceted_cid = cid;
			} */
			changeCateId(event){
				//1.设置选中的分类
				// console.log(event)//event 第一个参数就代表事件对象
				let target = event.currentTarget;
				console.log(target.dataset.cid)
				let cid = target.dataset.cid;
				this.selceted_cid = cid;
				//获取列表
				this.getList();
			},
			async getList(){
				//2.请求api获取数据
				//Promise化+async/await
				let cid = this.selceted_cid;
				let ret = await uni.request({url:`http://api.brqc.com.cn/shxmp/cate_list/${cid}`})
				// url:"http://127.0.0.1:3000/shxmp/cate",
				// let ret = await uni.request({url:`http://127.0.0.1:3000/shxmp/cate_list/${cid}`})
				console.log(ret[0]) //错误消息
				
				let data = ret[1].data.data;
				console.log(data)
				//3.把分类数据保存到store中
				/*  */
				// this.$store.commit("SETCATELIST",data)
				this.SETCATELIST(data);
				
			},
			subCart(event){
				let current = event.currentTarget; //事件对象
				let data = current.dataset;//自定义属性
				console.log(data.proid)
				let proid = data.proid;
				
				//购物车
				let cart = this.cart; //购物车保存到一个变量中
				let new_pro = cart.list.map(item=>{
					//修改当前产品数量
					if(item.id == proid){
						item.num = item.num -1;
					}
					return item;
				})
			    
				// 总价
				let total=0;
				let sum = new_pro.reduce((total,item,index,arr)=>{
					return total+(item.num * item.price) //累加
				},0)
				console.log(sum)
			    cart.total_num = new_pro.length;//总数
				cart.total_price = sum;
				//总数，总价
			    this.SETCART(cart)
			},
			addCart(event){
				//获取当前点击的产品
				var current = event.currentTarget; //事件对象
				var data = current.dataset;//自定义属性
				console.log(data.pro)
				//加入购物车
				//空购物车
				/* let cart = {
					list:[
						//产品列表数组
					],
					total_num:0,//总价
					total_price:0//总价
				} */
				console.log(this.cart)
				// 0.判断产品是否在购物车中
				var add_pro = data.pro; //要添加的商品
				var cart_pro = this.cart.list; //购物车中的 产品
				
				//true-存在，false-不存在
				let is_exits = cart_pro.some(item=>item.id == add_pro.id); 
				console.log(is_exits)
				// A:不在，直接加]
				
				let new_cart_pro = cart_pro;//产品列表数组(从store中取的)
				var cart = this.cart; //从store中拿到的购物车
				
				if(is_exits){
					console.log("存在，修改数量")
					new_cart_pro = cart_pro.map(item=>{
						//修改指定id的产品数量
						if(item.id == add_pro.id){
						    item.num = item.num+1;	
						}
						return item;//返回
					})
					console.log("修改后的数量：",new_cart_pro)
				}else{
					console.log("不在，直接加")
					add_pro.num = 1;           //修改数量
					new_cart_pro.push(add_pro);//把当前产品加入到 购物车 产品列表中
				}

				
				//C：保存到vuex中
				// var cart = this.cart;
				cart.list = new_cart_pro;//更新产品列表
				cart.total_num = new_cart_pro.length;//总数
				// 总价
				let total=0;
				let sum = new_cart_pro.reduce((total,item,index,arr)=>{
					return total+(item.num * item.price) //累加
				},0)
				console.log(sum)
				cart.total_price = sum;
				//总数，总价
				this.SETCART(cart)
				
			}
		},
		async onShow(){
			//获取分类数据
			//Promise化+async/await
			let ret = await uni.request({
				url:"http://api.brqc.com.cn/shxmp/cate",
				// url:"http://127.0.0.1:3000/shxmp/cate",
				method:"get"
			})
			let data = ret[1].data.data || []
			console.log(ret)
			this.cate_list = data;
			
			//获取列表
			this.getList();
		}
	}
</script>

<style lang="less">
.around{
	background-color: rgb(248, 248, 248);
	height: 100%;
	padding: 20rpx;
	
	.list{
		display: flex;
		padding-bottom: 120rpx;
		
		.left{
			flex: 0 0 150rpx;
			.active{
				color: orangered;
				background-color: #ffffff;
			}
			view{
				padding: 15rpx;
			}
		}
		
		.right{
			flex: 1;
			padding:15rpx;
			background: #ffffff;
			font-size: 30rpx;
			.item{
				//里面3大块水平排列
				display: flex;
				// justify-content: space-between;
				// align-items: center;
				.pic{
					flex: 0 0 100rpx;
					image{
						width: 160rpx;
						height: 160rpx;
						margin-right: 10rpx;
					}
				}
				.center{
					.price{
						text{
							color:orangered
						}
					}
				}
				//按钮大盒子，绝对定位到右下角
				position: relative;
				.btn{
					position: absolute;
					right: 0;
					bottom: 0;
					//加，减、数量 垂直居中
					display: flex;
					align-items: center;
					image{
						width: 64rpx;
						height: 64rpx;
					}
					
					text{
						padding:0 10rpx;
					}
				}
			}
			
		}
	}

	.cart{
		width: 100%;
		height: 120rpx;
		
		position: fixed;
		left: 0;
		bottom:0;
		
		//改变里面元素排列
		display: flex;
		line-height: 120rpx;
		
		.left{
			padding: 0 15rpx;
			height: 120rpx;
			flex: 2;
			background-color: rgb(248, 248, 248);
		}
		.right{
			height: 120rpx;
			flex: 1;
			background-color: rgb(253, 110, 89);
			text-align: center;
			color: #ffffff;
		}
	}
}
</style>
